﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 using Common.Extension;
 using Common.Model;
using Common.util;
 using Newtonsoft.Json;

namespace HotTopic.service
{
    public class TopicNameRedisDbProvider
    {
        public static readonly String SoftwareTopicStocksAdd = "topics:software:topic:stocks:add:";
        public static readonly String SoftwareTopicStocksDrop = "topics:software:topic:stocks:drop:";
        
        public static readonly String SoftwareUnProcessStocks = "topics:software:stocks:samename:unprocess:";
        public static readonly String SoftwareProcessStocks = "topics:software:stocks:samename:process:";
        
        public static readonly String SoftwareDifferNameNonJoin = "topics:software:stocks:topic:name:nonjoin:";
        public static readonly String SoftwareDifferNameJoinTopic = "topics:software:topic:name:join:";
        
        public static readonly String SoftwareTopicHis = "topics:software:topic:his:";
        
        public static readonly String SoftwareNewTopicDrop = "topics:software:topic:new:drop";
        
        public static readonly String SoftwareTopicDrop = "topics:software:topic:drop";
        public static readonly String SoftwareTopicAdd = "topics:software:topic:add";
        
        public static readonly String SoftwareNewTopic = "topics:software:topic:new";
        
        public static readonly String SoftwareTopicMain = "topics:software:topic:main";

        //获取需要添加的股票代码
        //相似主题使用主题名生成的key
        public static HashSet<string> ListTopicStocksAdd(string topicName)
        {
            String key = SoftwareTopicStocksAdd + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }
        
        //获取软件历史主题
        public static Dictionary<string,Topic> ListHisTopics(string sofwareName)
        {
            String key = SoftwareTopicHis + sofwareName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }
        
        //获取达信中需要删除的概念
        public static Dictionary<string,Topic> ListTopicDrop()
        {
            String key = SoftwareTopicDrop;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }
        
        //获取达信中需要删除的概念
        public static Dictionary<string,Topic> ListTopicAdd()
        {
            String key = SoftwareTopicAdd;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }

        //获取不需要合并的主题列表
        public static HashSet<string> ListNonjoinderTopics()
        {
            String key = SoftwareDifferNameNonJoin;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<string>>(result);
        }
        
        //获取需要去除的新增主题
        public static Dictionary<string,Topic> ListNewTopicDrop()
        {
            String key = SoftwareNewTopicDrop;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }
        
        //获取主板块
        public static Dictionary<string,Topic> ListTopicMain()
        {
            String key = SoftwareTopicMain;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }
        
        //获取合并后的主题名称表
        public static HashSet<string> ListJoinderTopics()
        {
            String key = SoftwareDifferNameJoinTopic;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<string>>(result);
        }
        
        //获取新增主题列表
        public static Dictionary<string,Topic> ListNewTopic()
        {
            String key = SoftwareNewTopic;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,Topic>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,Topic>>(result);
        }

        //获取需要丢弃的股票代码
        //相似主题使用主题名生成的key
        public static HashSet<string> ListDropStocks(string topicName)
        {
            String key = SoftwareTopicStocksDrop + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }
        
        //获取未处理的差异股票代码，合并主题时调用
        //相似主题使用主题名生成的key
        public static HashSet<Stock> ListUnProcessNewStocks(string topicName)
        {
            String key = SoftwareUnProcessStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<Stock>();
            }
            return JsonConvert.DeserializeObject<HashSet<Stock>>(result);
        }
        
        //获取已处理的差异股票代码，合并主题时调用
        //相似主题使用主题名生成的key
        public static HashSet<Stock> ListProcessedStocks(string topicName)
        {
            String key = SoftwareProcessStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<Stock>();
            }
            return JsonConvert.DeserializeObject<HashSet<Stock>>(result);
        }
        
        //保存需要删除的概念
        public static void CacheTopicsDrop(Dictionary<string,Topic> tdxTopicsDrop)
        {
            if (tdxTopicsDrop.Count <= 0)
            {
                return;
            }
            String key = SoftwareTopicDrop ;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(tdxTopicsDrop));
        }
        
                
        //保存需要删除的概念
        public static void AddTopicsDrop(Dictionary<string,Topic> tdxTopicsDrop)
        {
            var listTopicDrop = ListTopicDrop();
            listTopicDrop.AddRange(tdxTopicsDrop, true);
            if (tdxTopicsDrop.Count <= 0)
            {
                return;
            }
            String key = SoftwareTopicDrop ;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(listTopicDrop));
        }
        
        //保存需要增加的概念
        public static void CacheTopicsAdd(Dictionary<string,Topic> tdxTopicsDrop)
        {
            if (tdxTopicsDrop.Count <= 0)
            {
                return;
            }
            String key = SoftwareTopicAdd ;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(tdxTopicsDrop));
        }
        
        //保存需要添加的股票代码，人工处理调用
        //相似主题使用主题名生成的key
        public static void CacheTopicStocksAdd(String topicName, HashSet<string> acceptStocks)
        {
            if (acceptStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareTopicStocksAdd + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(acceptStocks));
        }
        
        //保存未处理的差异股票代码，合并主题时调用并更新该记录
        //相似主题使用主题名生成的key
        public static void CacheUnProcessNewStocks(string topicName, HashSet<Stock> differStocks)
        {
            var listProcessedStocks = ListProcessedStocks(topicName);
            listProcessedStocks.AddRange(differStocks.Except(listProcessedStocks));
            if (differStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareUnProcessStocks + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(listProcessedStocks));
        }
        
        //保存不需要合并的主题列表，人工处理后存储
        public static void CacheNonjoinderTopics(HashSet<string> topics)
        {
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameNonJoin;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存需要删除的主题，人工处理后存储
        public static void CacheNewTopicDrop(Dictionary<string,Topic> topics)
        {
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareNewTopicDrop;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存差分得到的新的主题
        public static void CacheNewTopic(Dictionary<string,Topic> topics)
        {
            var listNewTopic = ListNewTopic();
            listNewTopic.AddRange(topics, true);
            
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareNewTopic;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(listNewTopic));
        }
        
        //保存主板块
        public static void CacheMainTopic(Dictionary<string,Topic> topics)
        {
//            if (topics.Count <= 0)
//            {
//                return;
//            }
            String key = SoftwareTopicMain;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存软件历史主题
        public static void CacheHisTopics(string sofwareName,Dictionary<string,Topic> topics)
        {
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareTopicHis + sofwareName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
            Console.Write("ddddfff");
            RedisUtils.HtRedis.GetDatabase().StringSet(key + "Now", JsonConvert.SerializeObject(topics.ToDictionary(p => p.Key, p => p.Value.Stocks.Select(b => b.StockCode).ToList())));
        }
        
        //保存合并主题后的名字，人工处理后存储
        public static void CacheJoinderTopic(HashSet<string> topics)
        {
            var listJoinderTopicsName = ListJoinderTopics();
            listJoinderTopicsName.UnionWith(topics);
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameJoinTopic;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存合并主题后的名字，人工处理后存储
        public static void CacheJoinderTopic(string topicKey)
        {
            var listJoinderTopicsName = ListJoinderTopics();
            if (!listJoinderTopicsName.Contains(topicKey))
            {
                listJoinderTopicsName.Add(topicKey);
            }
            if (listJoinderTopicsName.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameJoinTopic;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(listJoinderTopicsName));
        }
        
        //保存已处理的差异股票代码，人工筛选时调用
        //相似主题使用主题名生成的key
        public static void CacheProcessedStocks(string topicName, HashSet<string> differStocks)
        {
            if (differStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareProcessStocks + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(differStocks));
        }
    }
}